/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Peer;

import FileStructure.Response;
import java.net.*;
import java.io.*;
import java.util.ArrayList;
/**
 *
 * @author bangyo17
 */
public class NewClientRequest{
    //HTTP GET request
    private Socket socket;
  private BufferedReader in;
  private PrintWriter out;

  //get response
 
  private static Response resp = new Response();


  public static Response getResponse(){
      return resp;
  }

  public NewClientRequest(InetAddress addr, 
                        String filename, int port, long uploaded, long downloaded, long left, String event) {
    
    try {
      socket =
        new Socket(addr, port);
    } catch(IOException e) {
      // If the creation of the socket fails,

        // nothing needs to be cleaned up.
    }
    try {
      in =
        new BufferedReader(
          new InputStreamReader(
            socket.getInputStream()));
      // Enable auto-flush:
      out =
        new PrintWriter(
          new BufferedWriter(
            new OutputStreamWriter(
              socket.getOutputStream())), true);
      out.println(filename+";"+port+";"+uploaded+";"+downloaded+";"+left+";"+event);
      //baca 
      //String input = in.readLine();

      run();
    } catch(IOException e) {
      // The socket should be closed on any
      // failures other than the socket
      // constructor:
      try {
        socket.close();
      } catch(IOException e2) {}
    }
    // Otherwise the socket will be closed by
    // the run() method of the thread.
  }
  public void run() {
    try {
        //receive response
        String str = in.readLine();
        //cek response, error apa nggak
        //jika error, gak ada karakter ';' di inputstream
        boolean ketemu = false;
        for(int i=0; i<str.length(); i++){
            if (str.charAt(i) == ';'){
                ketemu = true;
            }
        }

        if (ketemu){ //jika nggak error
            String[] parsed = str.split(";");

            resp.failure_reason = parsed[0];
            resp.warning_message = parsed[1];
            resp.interval = Integer.parseInt(parsed[2]);
            resp.complete = Integer.parseInt(parsed[3]);
            resp.incomplete = Integer.parseInt(parsed[4]);

            //parsing array of peer
            String[] parsed2 = parsed[5].split("#");
            //misal parsed2[0] = 127.0.0.1+1234

            if (parsed[5] != null){
                for (int i=0; i < parsed2.length; i++){
                    String[] parsed3 = parsed2[i].split("&");
                    resp.peers.clear();
                    resp.addPeer(parsed3[0], Integer.parseInt(parsed3[1]));
                   //System.out.println(resp.peers.get(i).IP + " " + resp.peers.get(i).Port);
                }
            }

        } //out.println("---END---");
        else{
            resp.interval = -1;
        }
    } catch(IOException e) {
    } finally {
      // Always close it:
      try {
        socket.close();
      } catch(IOException e) {}
      //threadcount--; // Ending this thread
    }
  }
  
}
